package com.speechb.common.text;

import java.text.MessageFormat;

/**
 * @author : llj
 * @version :2.1.0
 * @date : 2020/7/5 9:18
 */
public class PageUtil {

    public static String getPageInd(int pageNum, Object id, int pageCount, String baseUrl) {
        return getPageInd(pageNum, id, pageCount, baseUrl, null);
    }

    /**
     * 获取分页html
     *
     * @param pageNum 页码
     * @param id 类型id,可以为空
     * @param pageCount 总页数
     * @param baseUrl 基本页码
     * @return html页码
     */
    public static String getPageInd(int pageNum, Object id, int pageCount, String baseUrl, String param) {
        StringBuilder sb = new StringBuilder();
        StringBuilder parasSB = new StringBuilder();
        sb.append("<ul class='pagination'>");
        parasSB.append(baseUrl);
        if (id != null) {
            parasSB.append(id).append("/");
        }
        // 有上一页
        int length = parasSB.length();
        if (pageNum == 1) {
            // 第一页没有上一页
            sb.append(formatMsg("disabled", "#", "上一页"));
            sb.append(formatMsg("disabled", "#", "1"));
        } else {
            parasSB.append(pageNum - 1);
            if (param != null) {
                parasSB.append("?").append(param);
            }
            sb.append(formatMsg("prev", parasSB.toString(), "上一页"));
            parasSB.setLength(length);
            parasSB.append(1);
            if (param != null) {
                parasSB.append("?").append(param);
            }
            sb.append(formatMsg("first", parasSB.toString(), "1"));
        }
        if (pageNum > 2) {
            sb.append(formatMsg("disabled", "#", "......"));
            parasSB.setLength(length);
        }
        if (pageCount <= 4) {
            // 没有省略符
            numIndex(pageNum, 2, pageCount, sb, parasSB, length, param);
        } else if (pageCount - pageNum <= 2) {
            numIndex(pageNum, pageCount - 3, pageCount-1, sb, parasSB, length, param);
        } else {
            // 右省略符
            numIndex(pageNum, pageNum, pageNum + 3, sb, parasSB,length, param);
            sb.append(formatMsg("disabled", "#", "......"));
        }
        if (pageCount == pageNum || pageCount == 0) {
            // 最后一页
            sb.append(formatMsg("disabled", "#", "下一页"));
        } else {
            if (pageCount > 4) {
                // 最后一页
                parasSB.setLength(length);
                parasSB.append(pageCount);
                sb.append(formatMsg("last", parasSB.toString(), pageCount));
            }
            parasSB.setLength(length);
            parasSB.append(pageNum + 1);
            sb.append(formatMsg("next", parasSB.toString(), "下一页"));
        }
        sb.append("</ul>");
        return sb.toString();
    }

    /**
     * 给数字分页赋值
     * @param pageNum 当前页数
     * @param start 起始页
     * @param end 结束页
     * @param sb 内容
     * @param parasSB 中间内容
     */
    private static void numIndex(int pageNum,int start, int end, StringBuilder sb, StringBuilder parasSB, int length, String param) {
        // 左省略符
        for (int i = start; i <= end; i++) {
            if (i == 1) {
                continue;
            }
            parasSB.setLength(length);
            parasSB.append(i);
            if (param != null) {
                parasSB.append("?").append(param);
            }
            if (i == pageNum) {
                sb.append(formatMsg("page active disabled", parasSB.toString(), i));
            } else {
                sb.append(formatMsg("page", parasSB.toString(), i));
            }
        }
    }

    private static String formatMsg(String className, String href, Object name) {
        return MessageFormat.format("<li class=\"{0}\"><a href=\"{1}\">{2}</a></li>", className, href, name);
    }

}
